package com.norteksoft.erm.model;

import java.util.HashSet;
import java.util.Set;
import org.apache.commons.lang.builder.HashCodeBuilder;

/**
 * 数据域定义
 * 
 * @author Jiffy
 * @hibernate.class table="ERM_DATADOMAIN"
 */

public class DataDomain extends BaseObject {

	private static final long serialVersionUID = 42108606635103510L;

	public static final String OPERATOR_EQ = "eq";

	public static final String OPERATOR_PREFIX = "prefix";

	public static final String OPERATOR_POSTFIX = "postfix";

	public static final String OPERATOR_LT = "lt";

	public static final String OPERATOR_GT = "gt";

	public static final String OPERATOR_BETWEEN = "between";
	
	public static final String OPERATOR_ISNULL = "isnull";

	private Long OID = new Long(-1);// OID

	private String name;// 数据域名称

	private DataDimension dataDimension;// 所属条件

	private String operator;// 关系运算符

	private String value1;// 属性值1

	private String value2;// 属性值2

	private String remark;// 备注

	private Set dataPermissions = new HashSet();// 数据域授权许可集

	public int hashCode() {
		return new HashCodeBuilder().append(OID).append(name).append(operator)
				.append(value1).append(value2).append(remark).toHashCode();
	}

	/**
	 * @hibernate.id column="OID" unsaved-value="-1" type="long"
	 * @hibernate.generator class="sequence"
	 * @hibernate.param name="sequence" value="ERM_SEQUENCE"
	 */

	public Long getOID() {
		return OID;
	}

	public void setOID(Long oid) {
		OID = oid;
	}

	/**
	 * @hibernate.property column="NAME" length="60" type="string"
	 *                     not-null="true"
	 * @return String
	 */
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @hibernate.many-to-one name="dataDimension" column="DATADIMENSIONOID"
	 *                        class="com.norteksoft.erm.model.DataDimension"
	 */
	public DataDimension getDataDimension() {
		return dataDimension;
	}

	public void setDataDimension(DataDimension dataDimension) {
		this.dataDimension = dataDimension;
	}

	/**
	 * @hibernate.property column="OPERATOR" length="60" type="string"
	 * @return String
	 */
	public String getOperator() {
		return operator;
	}

	public void setOperator(String operator) {
		this.operator = operator;
	}

	/**
	 * @hibernate.property column="VALUE1" length="90" type="string"
	 * @return String
	 */

	public String getValue1() {
		return value1;
	}

	public void setValue1(String value1) {
		this.value1 = value1;
	}

	/**
	 * @hibernate.property column="VALUE2" length="90" type="string"
	 * @return String
	 */
	public String getValue2() {
		return value2;
	}

	public void setValue2(String value2) {
		this.value2 = value2;
	}

	/**
	 * @hibernate.set lazy="true" inverse="true" cascade="all-delete-orphan"
	 *                order-by="authorityType asc"
	 * @hibernate.key column="DATADOMAINOID"
	 * @hibernate.one-to-many class="com.norteksoft.erm.model.DataPermission"
	 * @return Set
	 */
	public Set getDataPermissions() {
		return dataPermissions;
	}

	public void setDataPermissions(Set dataPermissions) {
		this.dataPermissions = dataPermissions;
	}

	public String getRule() {
		StringBuffer sbf = new StringBuffer();
		if (OPERATOR_BETWEEN.equals(operator)) {
			sbf.append("'");
			sbf.append(value1);
			sbf.append("'");
			sbf.append("<");
			sbf.append(dataDimension.getAttribute());
			sbf.append("<");
			sbf.append("'");
			sbf.append(value2);
			sbf.append("'");
		} else {
			sbf.append(dataDimension.getAttribute());
			if (OPERATOR_PREFIX.equals(operator)) {
				sbf.append(" like '");
				sbf.append(value1);
				sbf.append("%'");
			} else if (OPERATOR_POSTFIX.equals(operator)) {
				sbf.append(" like '%");
				sbf.append(value1);
				sbf.append("'");
			} else if (OPERATOR_LT.equals(operator)) {
				sbf.append(" < '");
				sbf.append(value1);
				sbf.append("'");
			} else if (OPERATOR_GT.equals(operator)) {
				sbf.append(" > '");
				sbf.append(value1);
				sbf.append("'");
			} else if (OPERATOR_EQ.equals(operator)) {
				sbf.append(" = '");
				sbf.append(value1);
				sbf.append("'");
			}
		}
		return sbf.toString();
	}

	/**
	 * @hibernate.property column="REMARK" length="1000" type="string"
	 * @return String
	 */
	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}
}
